package 文档教程

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"time"
)

type UserModel struct {
	ID        int    `json:"id"`
	UserName  string `json:"user_name"`
	Age       int    `json:"age"`
	NickName  string `json:"nick_name"`
	CreatedAt string `json:"created_at"`
	Title     string `json:"title"`
}

func main() {
	client, err := elastic.NewClient(
		elastic.SetURL("http://192.168.222.154:9200"),
		elastic.SetSniff(false),
		elastic.SetBasicAuth("", ""),
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	user := UserModel{
		ID:        12,
		UserName:  "lisi",
		Age:       23,
		NickName:  "夜空中最亮的lisi",
		CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
		Title:     "今天天气很不错",
	}
	indexResponse, err := client.Index().Index("user_index").BodyJson(user).Do(context.Background())
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%#v\n", indexResponse)

}
